package com.whu;

import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;

public class Toolbar extends ToolBar {

    private MainView mainView;

    public Toolbar(MainView mainView) {
        this.mainView = mainView;
        Button draw = new Button("绘制");
        draw.setOnAction(this::handleDraw);

        Button erase = new Button("擦除");
        erase.setOnAction(this::handleErase);
        
        Button step = new Button("进程");
        step.setOnAction(this::handleStep);
        
        Button reset = new Button("重置进程");
        reset.setOnAction(this::handeReset);

        Button start = new Button("开始");
        start.setOnAction(this::handeStart);

        Button stop = new Button("停止");
        stop.setOnAction(this::handeStop);

        // 添加到画布
        this.getItems().addAll(draw, erase, step, reset, start, stop);
    }



    private void handleStep(ActionEvent stepEvent) {
        this.mainView.getSimulation().step();
        this.mainView.draw();
        this.mainView.setApplicationState(MainView.SIMULATING);
    }

    private void handleErase(ActionEvent eraseEvent) {
        this.mainView.setDrawMode(Simulation.DEAD);
    }

    private void handleDraw(ActionEvent drawEvent) {
        this.mainView.setDrawMode(Simulation.ALIVE);
    }

    private void handeStop(ActionEvent actionEvent) {
        this.mainView.getSimulator().stop();
    }

    private void handeStart(ActionEvent actionEvent) {
        this.mainView.setApplicationState(MainView.SIMULATING);
        this.mainView.getSimulator().start();
    }

    private void handeReset(ActionEvent actionEvent) {
        this.mainView.setApplicationState(MainView.EDITING);
        this.mainView.draw();
    }
}
